﻿Public Class TemplateEntry
    Dim vParamsField As String()
    Public Property ParamField() As String()
        Get
            Return vParamsField
        End Get
        Set(ByVal value As String())
            vParamsField = value
        End Set
    End Property
    Dim vFieldControl As Control
    Public Property FieldControl() As Control
        Get
            Return vFieldControl
        End Get
        Set(ByVal value As Control)
            vFieldControl = value
        End Set
    End Property
    Dim DGV As DataGridView
    Public Property Datagriddetail() As DataGridView
        Get
            Return DGV
        End Get
        Set(ByVal value As DataGridView)
            DGV = value
        End Set
    End Property

    Dim vProcExecute As String
    Public Property ProcExecute() As String
        Get
            Return vProcExecute
        End Get
        Set(ByVal value As String)
            vProcExecute = value
        End Set
    End Property

    Sub setbutton(ByVal execname As String, ByVal active As Boolean)
        Dim btnname() As String
        btnname = New String() {"Add", "Edit", "Delete"}

        For Each ctl As Control In PnlBtn.Controls
            If TypeOf ctl Is Button Then
                If ctl.Tag = btnname(0) Or ctl.Tag = btnname(1) Or ctl.Tag = btnname(2) Then
                    If active Then
                        ctl.Enabled = False
                    Else
                        ctl.Enabled = True
                    End If
                    If ctl.Tag = execname Then
                        If active Then
                            ctl.Enabled = True
                            ctl.Text = "Save"
                        Else
                            ctl.Text = ctl.Tag
                        End If
                    Else
                        ctl.Text = ctl.Tag
                    End If
                End If
            End If
        Next

    End Sub

    Overridable Function validationAdd() As Boolean
        Return validationAdd
    End Function
    Overridable Function validationEdit() As Boolean
        Return validationEdit
    End Function
    Overridable Function validationDel() As Boolean
        Return validationDel
    End Function
    Dim ParamFinder() As Object
    Public Property ParamCari() As Object()
        Get
            Return ParamFinder
        End Get
        Set(ByVal value As Object())
            ParamFinder = value
        End Set
    End Property
    Overridable Sub ParamFind()

    End Sub
    Sub searchitem()
        Dim dtitem As New DataTable
        'Dim obj() As Object
        'If finder = "all" Then
        '    obj = New Object() {"*", "vitem", "namaitem", ""}
        'Else
        '    obj = New Object() {"*", "vitem", ComCariBy.Text, txtKunci.Text}
        'End If
        If Utils.executeSP("proc_searchall", ParamFinder, dtitem) Then
            If dtitem IsNot Nothing Then
                DGV.DataSource = dtitem
            End If
        End If
    End Sub
    Overridable Sub RefreshValue()
        For Each ctl As Control In vFieldControl.Controls
            If TypeOf ctl Is TextBox Then
                If TypeOf ctl Is AMS.TextBox.NumericTextBox Then
                    Dim cnum As AMS.TextBox.NumericTextBox
                    cnum = CType(ctl, AMS.TextBox.NumericTextBox)
                    cnum.Text = 0
                Else
                    ctl.Text = ""
                End If
                ctl.Enabled = False
            ElseIf TypeOf ctl Is ComboBox Then
                Dim cb As ComboBox
                cb = CType(ctl, ComboBox)
                cb.Enabled = False
            ElseIf TypeOf ctl Is CheckBox Then
                Dim cbx As CheckBox
                cbx = CType(ctl, CheckBox)
                cbx.Checked = False
                cbx.Enabled = False
            End If
        Next
        ParamFind()
        searchitem()
        setbutton("", False)
        DGV.Enabled = True

    End Sub
    Function setparamitem(ByVal exec As String) As Object()
        Dim param As New List(Of Object)

        param.Clear()
        param.Add(exec)
        For Each s As String In vParamsField
            For Each ctl As Control In vFieldControl.Controls
                If ctl.Name = s Then

                    If TypeOf ctl Is TextBox Then
                        If TypeOf ctl Is AMS.TextBox.NumericTextBox Then
                            Dim cnum As AMS.TextBox.NumericTextBox
                            cnum = CType(ctl, AMS.TextBox.NumericTextBox)
                            If cnum.Text = "" Then cnum.Text = 0
                            param.Add(CDbl(cnum.NumericText))
                        Else
                            param.Add(IIf(ctl.Text = "", Convert.DBNull, ctl.Text))
                        End If
                    ElseIf TypeOf ctl Is ComboBox Then
                        Dim cbx As ComboBox
                        cbx = CType(ctl, ComboBox)
                        If cbx.DataSource IsNot Nothing Then
                            param.Add(cbx.SelectedValue)
                        Else
                            param.Add(cbx.Text)
                        End If
                    ElseIf TypeOf ctl Is CheckBox Then
                        Dim cbx As CheckBox
                        cbx = CType(ctl, CheckBox)

                        param.Add(IIf(cbx.Checked, 1, 0))
                    End If
                End If
            Next
        Next
        Return param.ToArray
    End Function

    Sub ExecItem(ByVal execname As String)
        Dim dt As New DataTable
        If Utils.executeSP(vProcExecute, setparamitem(execname), dt) Then
            If dt IsNot Nothing Then
                If dt.Rows.Count > 0 Then
                    MsgBox(dt(0)(0))
                End If
            End If

        End If
    End Sub

    Sub setentry(ByVal active As Boolean)
        If active Then
            For Each ctl As Control In vFieldControl.Controls
                If TypeOf ctl Is TextBox Then
                    ctl.Enabled = True
                ElseIf TypeOf ctl Is ComboBox Then
                    Dim cb As ComboBox
                    cb = CType(ctl, ComboBox)
                    cb.Enabled = True
                ElseIf TypeOf ctl Is CheckBox Then
                    Dim cbx As CheckBox
                    cbx = CType(ctl, CheckBox)
                    cbx.Enabled = True
                End If
            Next
            DGV.Enabled = False
        Else
            For Each ctl As Control In vFieldControl.Controls
                If TypeOf ctl Is TextBox Then
                    ctl.Text = ""
                    ctl.Enabled = False
                ElseIf TypeOf ctl Is ComboBox Then
                    Dim cb As ComboBox
                    cb = CType(ctl, ComboBox)
                    cb.Enabled = False
                ElseIf TypeOf ctl Is CheckBox Then
                    Dim cbx As CheckBox
                    cbx = CType(ctl, CheckBox)
                    cbx.Checked = False
                    cbx.Enabled = False
                End If
            Next
            DGV.Enabled = True
        End If
    End Sub

    Public Sub DGV_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        If e.RowIndex = -1 Then
            Exit Sub
        End If


        For Each ctl As Control In vFieldControl.Controls
            If TypeOf ctl Is TextBox Then
                If TypeOf ctl Is AMS.TextBox.NumericTextBox Then
                    Dim cnum As AMS.TextBox.NumericTextBox
                    cnum = CType(ctl, AMS.TextBox.NumericTextBox)
                    ctl.Text = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), "0", DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                Else
                    ctl.Text = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), "", DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                End If

            ElseIf TypeOf ctl Is ComboBox Then
                Dim cb As ComboBox
                cb = CType(ctl, ComboBox)
                If cb.DataSource Is Nothing Then
                    cb.Text = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), "", DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                Else
                    cb.SelectedValue = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), 0, DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                End If

            ElseIf TypeOf ctl Is CheckBox Then
                Dim cbx As CheckBox
                cbx = CType(ctl, CheckBox)
                If Not cbx.Tag = "" Then
                    cbx.Checked = IIf(IsDBNull(DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value), 0, DGV(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                End If
            End If
        Next

    End Sub

    Private Sub btnAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItem.Click
        If btnAddItem.Text = btnAddItem.Tag Then
            RefreshValue()
            setentry(True)
            setbutton("Add", True)
        Else
            If Not validationAdd() Then
                If MessageBox.Show("Maaf Data Entry Tidak Valid !!! " & vbCrLf & "Apakah Akan Diulangi ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                    Exit Sub
                End If
            End If
            ExecItem("insert")
            RefreshValue()
        End If
    End Sub

    Private Sub btnEditItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditItem.Click
        If btnEditItem.Text = btnEditItem.Tag Then
            'btnRefreshItem.PerformClick()
            setentry(True)
            setbutton("Edit", True)
        Else
            If Not validationEdit() Then
                If MessageBox.Show("Maaf Data Entry Tidak Valid !!! " & vbCrLf & "Apakah Akan Diulangi ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                    Exit Sub
                End If
            End If
            ExecItem("update")
            RefreshValue()
        End If
    End Sub

    Private Sub btnDelItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelItem.Click
        If Not validationDel() Then
            MessageBox.Show("Silahkan Pilih Data Untuk Delete !!!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        Else
            If MessageBox.Show("Apakah Anda Yakin Akan Menghapus Data ? ", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                ExecItem("delete")
                RefreshValue()
            End If
        End If
    End Sub

    Private Sub btnRefreshItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefreshItem.Click
        RefreshValue()
    End Sub

    Private Sub btnCloseItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCloseItem.Click
        Me.Close()
    End Sub

    Private Sub Panel4_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel4.Paint

    End Sub

    Private Sub TemplateEntry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class